﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.DTO;
using QuanLyKhachSan.BUS;

namespace QuanLyKhachSan
{
    public partial class frmQuanLyKhachHang : Form
    {
        //bien luu tru cho biet Thao Tac(1: them, 2: update,3:Xoa)
        int thaoTac = 0;
        List<KhachHangDTO> dsKhachHangdto = new List<KhachHangDTO>();
        
        public frmQuanLyKhachHang()
        {
            InitializeComponent();
        }
        // Kiem tra du lieu nhap vao
        private bool KiemTraDuLieuNhap()
        {
            // Ten khach hang ko duoc de trong
            if (string.IsNullOrEmpty(txtTen.Text) == true )
            {
                MessageBox.Show("Ten Khach Hang khong duoc de trong ","Thong Bao");
                txtTen.Focus();
                return false;
            }
            // Dia chi khach hang khong duoc de trong
            if (string.IsNullOrEmpty(txtDiaChi.Text) == true )
            {
                MessageBox.Show("Dia Chi cua khach hang khong duoc de trong ","Thong Bao");
                txtDiaChi.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCMND.Text ) == true)
            {
                MessageBox.Show("CMND khong duoc de trong","Thong Bao");
                txtCMND.Focus();
                return false;
            }
            // Kiem tra ten khach hang
            if (DTO.KhachHangDTO.KiemTraHoTen(txtTen.Text) == false)
            {
                MessageBox.Show("Ten khach hang khong hop le ", "Thong bao ");
                txtTen.Focus();
                return false;
            }
           // Ho khach hang duoc de trong, co thong tin thi kiem tra tiep
           
            if (DTO.KhachHangDTO.KiemTraHoTen(txtHo.Text) == false)// neu co nhap Ho moi kiem tra
            {
                MessageBox.Show("Ho khach hang khong hop le ", "Thong bao ");
                txtHo.Focus();
                return false;
            }
            
            // Ten dem duoc de trong, neu co ten dem moi kiem tra
           
            if (DTO.KhachHangDTO.KiemTraHoTen(txtTenDem.Text) == false)
            {
                MessageBox.Show("Ten dem khong hop le ", "Thong bao ");
                txtTenDem.Focus();
                return false;
            }
           
            // CMND duoc de trong, neu co CMND thi kiem tra
            if (DTO.KhachHangDTO.KiemTraCMND(txtCMND.Text) == false)
            {
                MessageBox.Show("CMND khong hop le ", "Thong bao ");
                txtCMND.Focus();
                return false;
            }
        
            if (DTO.KhachHangDTO.KiemTraDiaChi(txtDiaChi.Text) == false)
            {
                MessageBox.Show("Dia chi khong hop le ", "Thong bao ");
                txtDiaChi.Focus();
                return false;
            }
            return true;
        }
        // Kiem tra ten va cmnd xem co truy hay khong
        public bool KiemTraKhongTrungDuLieu(KhachHangDTO khachHangdto)
        {
            foreach (KhachHangDTO kh in dsKhachHangdto)
            {
                
                if (kh.CMND1 == khachHangdto.CMND1)
                {
                    MessageBox.Show("So CMND khong duoc nhap trung", "Thong Bao");
                    txtTen.Focus();
                    return false;
                }
                        
            }
            return true;
        }
        public bool KiemTraCapNhat(string cmndBD, KhachHangDTO khachHangdto)
        {
            int i = 0;
            foreach (KhachHangDTO kh in dsKhachHangdto)
            {

                if (kh.CMND1 == cmndBD )
                {
                    dsKhachHangdto.RemoveAt(i);
                }
                i++;
            }
            return KiemTraKhongTrungDuLieu(khachHangdto);
        }
        // Lay thong tin khach hang
        public KhachHangDTO LayThongTinTuTextBox()
        {
            KhachHangDTO khachHangdto = new KhachHangDTO();
            if (string.IsNullOrEmpty(txtMaKhachHang.Text) == false)
            {
                khachHangdto.MaKhachHang = int.Parse(txtMaKhachHang.Text);
            }
            //khachHangdto.MaKhachHang = int.Parse(txtMaKhachHang.Text);
            khachHangdto.HoKhachHang = txtHo.Text;
            khachHangdto.TenLot = txtTenDem.Text;
            khachHangdto.TenKhachHang = txtTen.Text;
            khachHangdto.DiaChi = txtDiaChi.Text;
            khachHangdto.CMND1 = txtCMND.Text;
            khachHangdto.DanhDau = 0;
            LoaiKhachHangDTO loaiKhachHangdto = LoaiKhachHangBUS.LayLoaiKhachHangTheoTenLoaiKH(cboLoaiKhachHang.Text);
            khachHangdto.MaLoaiKH = loaiKhachHangdto.MaLoaiKH;


            return khachHangdto;
        }
        private void frmQuanLyKhachHang_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;
            // Lay danh sach cac khach hang trong he thong
            DataTable dtKH = new DataTable();
            dtKH = KhachHangBUS.LayDanhSachKH();
            if (dtKH.Rows.Count > 0)
            {
                for (int j = 0; j < dtKH.Rows.Count; j++)
                {
                    DataRow drKH = dtKH.Rows[j];
                    KhachHangDTO kh = new KhachHangDTO();
                    
                    kh.CMND1    = drKH["CMND"].ToString();
                     
                    dsKhachHangdto.Add(kh);
                }
            }

            //Hien thi cac button
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            btnThoát.Visible = true;
            btnHuy.Visible = false;

            // An cac textbox
            txtTen.Enabled = false;
            txtTenDem.Enabled = false;
            txtHo.Enabled = false;
            txtCMND.Enabled = false;
            txtDiaChi.Enabled = false;
            cboLoaiKhachHang.Enabled = false;
            btnLuu.Enabled = false;
            txtMaKhachHang.Text = "";
            txtTen.Text = "";
            txtHo.Text = "";
            txtTenDem.Text = "";
            txtCMND.Text = "";
            txtDiaChi.Text = "";
            // Hien thi thong tin len comboboxLoaiKhachHang
            DataTable dt = new DataTable();
            List<LoaiKhachHangDTO> dsLoaiKhachHang = new List<LoaiKhachHangDTO>();
            dt = LoaiKhachHangBUS.LayDSLoaiKH();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];

                    LoaiKhachHangDTO loaiKhachHangdto = new LoaiKhachHangDTO();
                    loaiKhachHangdto.MaLoaiKH  =int.Parse(dr["MaLoaiKhachHang"].ToString());
                    loaiKhachHangdto.TenLoai =dr["TenLoaiKhachHang"].ToString();

                    dsLoaiKhachHang.Add(loaiKhachHangdto);
                }
                cboLoaiKhachHang.ValueMember = "MaLoaiKH";
                cboLoaiKhachHang.DisplayMember = "TenLoai";
                cboLoaiKhachHang.DataSource = dsLoaiKhachHang ;
                cboLoaiKhachHang.SelectedIndex = 0;
            }

            
        }

        public void HienThiThongTin(KhachHangDTO khachHangdto)
        {
            DataTable dt = new DataTable();
            dt = KhachHangBUS.LayDanhSachKhachHangTheoTen(khachHangdto.TenKhachHang);
            dgvDanhSachKhachHang.DataSource = dt;

            //Hien thi cac thong tin
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            btnThoát.Visible = true;

            // An cac thong tin
            btnLuu.Enabled = false;
            btnHuy.Visible = false;

            txtTen.Enabled = false;
            txtTenDem.Enabled = false;
            txtHo.Enabled = false;
            txtCMND.Enabled = false;
            txtDiaChi.Enabled = false;
            cboLoaiKhachHang.Enabled = false;
            btnLuu.Enabled = false;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            //dang ky thao tac la them thaoTac =0
            thaoTac = 1;

            txtTen.Enabled = true;
            txtTenDem.Enabled = true;
            txtHo.Enabled = true;
            txtCMND.Enabled = true;
            txtDiaChi.Enabled = true;
            cboLoaiKhachHang.Enabled = true;
            btnLuu.Enabled = true;
            btnHuy.Visible = true;

            btnThem.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            btnThoát.Visible = false;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            //dang ky thao tac la update thaoTac =1
            thaoTac = 2;

            txtTen.Enabled = true;
            txtTenDem.Enabled = true;
            txtHo.Enabled = true;
            txtCMND.Enabled = true;
            txtDiaChi.Enabled = true;
            cboLoaiKhachHang.Enabled = true;
            btnLuu.Enabled = true;
            btnHuy.Visible = true;

            btnThem.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            btnThoát.Visible = false;
        }
        
       
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachKhachHang.SelectedRows.Count > 0)
            {
                bool kq = false;
                KhachHangDTO khachHangdto = new KhachHangDTO();
                khachHangdto.MaKhachHang = int.Parse(txtMaKhachHang.Text);
                DialogResult result = MessageBox.Show("Ban chac chan muon xoa khong?", "Thong Bao", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    kq = KhachHangBUS.Delete(khachHangdto);
                }
                if (kq == true)
                {
                    MessageBox.Show("Xoa Khach Hang thanh cong ", "Thong bao ");
                    HienThiThongTin(khachHangdto);
                }
                else
                {
                    MessageBox.Show("Xoa Khach Hang khong thanh cong ", "Thong bao ");
                    frmQuanLyKhachHang_Load(sender, e);
                }
            }
            else
                MessageBox.Show("Vui Long Chon Khach Hang Muon Xoa");
        }
       
        private void btnLuu_Click(object sender, EventArgs e)
        {
            // Kiem tra thong tin khi luu
            if (KiemTraDuLieuNhap() == true)
            {
                // xu ly
                bool kq = false ;
                KhachHangDTO khachHangdto = new KhachHangDTO();
                khachHangdto = LayThongTinTuTextBox();
                if (thaoTac == 1)//them
                {
                    if (KiemTraKhongTrungDuLieu(khachHangdto) == true)
                    {
                        kq = KhachHangBUS.Insert(khachHangdto);
                    }
                    else
                    {
                        kq = false;
                    }
                }
                else if (thaoTac == 2)//Cap nhat
                {
                    string cmndBD = dgvDanhSachKhachHang.SelectedRows[0].Cells[3].Value.ToString();
                    
                    if (KiemTraCapNhat(cmndBD ,khachHangdto )==true  )
                    {
                        kq = KhachHangBUS.Update(khachHangdto);
                    }
                    else
                    {
                        kq = false ;
                    }
                }
                if (kq == true)
                {
                    MessageBox.Show("Luu thong tin khach hang thanh cong!");
                    HienThiThongTin(khachHangdto);
                   
                }
                else
                {
                    MessageBox.Show("Luu thong tin khach hang khong thanh cong");
                    txtHo.Focus();//load lai form
                }
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            btnThem.Enabled = true;
            btnCapNhat.Enabled = true;
            btnXoa.Enabled = true;
            btnThoát.Visible = true;

            btnLuu.Enabled = false;
            btnHuy.Visible = false;
            txtHo.Enabled = false;
            txtTenDem.Enabled = false;
            txtTen.Enabled = false;
            cboLoaiKhachHang.Enabled = false;
            txtDiaChi.Enabled = false;
            txtCMND.Enabled = false;

        }
        private void btnThoát_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDanhSachKhachHang_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDanhSachKhachHang.SelectedRows.Count > 0)
            {
                txtMaKhachHang.Text = dgvDanhSachKhachHang.SelectedRows[0].Cells[0].Value.ToString();
                txtHo.Text = dgvDanhSachKhachHang.SelectedRows[0].Cells[1].Value.ToString();
                txtTenDem.Text = dgvDanhSachKhachHang.SelectedRows[0].Cells[2].Value.ToString();
                txtTen.Text = dgvDanhSachKhachHang.SelectedRows[0].Cells[3].Value.ToString();

                txtDiaChi.Text = dgvDanhSachKhachHang.SelectedRows[0].Cells[5].Value.ToString();
                txtCMND.Text = dgvDanhSachKhachHang.SelectedRows[0].Cells[6].Value.ToString();
                
                int maLoaiKH = int.Parse(dgvDanhSachKhachHang.SelectedRows[0].Cells[4].Value.ToString());
                LoaiKhachHangDTO loaiKhachHangdto = LoaiKhachHangBUS.LayLoaiKhachHangTheoMaLoai(maLoaiKH);
                cboLoaiKhachHang.Text = loaiKhachHangdto.TenLoai;
            }
            else
            {
                txtMaKhachHang.Text = "";
                txtTen.Text = "";
                txtHo.Text = "";
                txtTenDem.Text = "";
                txtCMND.Text = "";
                txtDiaChi.Text = "";
                
                
            }
        }

        private void txtTenTimKiem_TextChanged(object sender, EventArgs e)
        {
            string tenTK = txtTenTimKiem.Text;
            DataTable dt = new DataTable();
            dt = KhachHangBUS.LayDanhSachKhachHangTheoTen(tenTK);
            dgvDanhSachKhachHang.DataSource = dt;
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

    }
}